package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.NativeADViewHolder
import com.horoscope.astrology.zodiac.signs.free.bean.ContentBean
import com.horoscope.astrology.zodiac.signs.free.bean.NativeAdBean
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ad.ADConfig


/**
 *  作者    1024
 *  时间    2020-02-03 17:05
 *  文件    AstrologyMainAdapter
 *  描述    Astrology页展示
 */
class AstrologyMainAdapter(private val context: Context, val mDataList: ArrayList<ContentBean>) :
    BaseAdapter() {

    companion object {
        const val TEXT = 0x13
        const val AD = 0x14
        const val PB = "Personal birth\nchat"
        const val LF = "Love &\nFriendship"
        const val LZ = "Love Zodiac\ncompatibility"
        const val ZC = "Zodiac\ncompatibility"
        const val NC = "Name\ncompatibility"
        const val QU = "Quizzes"
        const val NU = "Numerology"
        const val PS = "Palm Scan"
    }

    override fun getItemCount(): Int {
        return mDataList.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (mDataList[position].title == "Ad") {
            AD
        } else {
            TEXT
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            AD -> {
                val view =
                    LayoutInflater.from(context).inflate(R.layout.ad_layout, parent, false)
                NativeADViewHolder(context, view)
            }
            else -> {
                val view =
                    LayoutInflater.from(context).inflate(R.layout.item_astrology, parent, false)
                MyViewHolder(view)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is MyViewHolder -> {
                if (mDataList.size > 0) {
                    val bean = mDataList[position]
                    //获取对应的shape实例
                    val myGrad = GradientDrawable()
                    myGrad.cornerRadius = 16f
                    when (bean.title) {
                        PB -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_1,
                                R.mipmap.astrology_personal
                            )
                        }
                        LF -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_2,
                                R.mipmap.astrology_friendly
                            )
                        }
                        LZ -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_3,
                                R.mipmap.astrology_love
                            )
                        }
                        ZC -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_4,
                                R.mipmap.astrology_zodiac
                            )
                        }
                        NC -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_5,
                                R.mipmap.astrology_name
                            )
                        }
                        QU -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_1,
                                R.mipmap.astrology_quizzes
                            )
                        }
                        NU -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_2,
                                R.mipmap.astrology_number
                            )
                        }
                        PS -> {
                            setItemBackground(
                                myGrad,
                                holder,
                                R.color.astrology_color_3,
                                R.mipmap.astrology_hand
                            )
                        }

                    }
                    holder.text.text = bean.title
                    holder.bg.background = myGrad
                    if (mOnItemClickedListener != null) {
                        holder.bg.setOnClickListener {
                            // 快速双击
                            if (!SysUtils.isFastClick()) {
                                mOnItemClickedListener!!.onItemClick(it, position, bean.title)
                            }
                        }
                    }

                }
            }
            is NativeADViewHolder -> {
                handleNativeAd(mDataList[position].nativeAdBean, holder, "Native_Astrology")
            }
        }
    }

    private fun setItemBackground(
        myGrad: GradientDrawable,
        holder: MyViewHolder,
        color: Int,
        img: Int
    ) {
        myGrad.setColor(ContextCompat.getColor(context, color))
        holder.image.setBackgroundResource(img)
    }

    private fun handleNativeAd(nativeAdBean: NativeAdBean?, holder: NativeADViewHolder, placement: String) {
        if (nativeAdBean != null) {
            holder.changeAD(nativeAdBean.adInfo.advertiser) // 通过advertiser判断显示那个原生广告视图
            when (nativeAdBean.adInfo.advertiser) {
                ADConfig.FB -> {
                    if (nativeAdBean.facebookNativeAD != null) {
                        holder.inflateFacebookAd(nativeAdBean.facebookNativeAD, "Astrology_Small", placement)
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
                ADConfig.ADMOB -> {
                    if (nativeAdBean.googleNativeAd != null) {
                        holder.inflateAdMobAd(nativeAdBean.googleNativeAd!!, "Astrology_Small")
                        holder.visibility()
                    } else {
                        holder.inVisibility()
                    }
                }
            }
        }
    }

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val bg: ConstraintLayout = itemView.findViewById(R.id.astrology_bg)
        val image: ImageView = itemView.findViewById(R.id.iv_img)
        val text: TextView = itemView.findViewById(R.id.tv_text)
    }

}